package com.ftg.learn.sys.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftg.learn.comm.entity.sys.Employee;
import com.ftg.learn.comm.entity.sys.UserRole;
import com.ftg.learn.sys.user.mapper.EmployeeMapper;
import com.ftg.learn.sys.user.mapper.UserMapper;
import com.ftg.learn.sys.user.mapper.UserRoleMapper;
import com.ftg.learn.sys.user.service.IEmployeeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author kangjx
 * @since 2022-10-30
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Resource
    UserMapper userMapper;

    @Resource
    UserRoleMapper userRoleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delEmployeeWithUser(Integer id) {

        //1 删除用户
        int ist1 = userMapper.deleteById(id);
        //2 删除用户角色
        QueryWrapper<UserRole> query = new QueryWrapper<>();
        query.eq("u_id",id);
        int ist2 = userRoleMapper.delete(query);
        //3 删除员工
        int ist3 = this.baseMapper.deleteById(id);
        return ist1 >= 0 && ist2 >= 0 && ist3 >= 0;
    }
}
